package dw2;
/**
 * @author Team DeathWorm
 */
import java.io.*;
import java.util.Arrays;
import java.util.Iterator;
import java.util.LinkedList;

abstract public class FileIO extends Thread {

    static private File file;
    static private LinkedList<User> listUsers = new LinkedList<User>() {

        public LinkedList<User> sort(int LIST_NUM) {
            LinkedList<User> sortedList = new LinkedList<User>();
            User user = null;
            while (sortedList.size() < LIST_NUM) {
                for (Iterator<User> iterator = this.iterator(); iterator.hasNext();) {
                    User user1 = iterator.next();
                    if ((user1.getScore() > user.getScore()) && ((user1.getUsername().compareTo(user.getUsername())) < 0)) {
                        user = user1;
                    }
                }
                sortedList.add(user);
            }
            return (listUsers = sortedList);
        }
    };
    private boolean Permission = true;
    static private boolean ListPermission = true;

    abstract public boolean read();

    abstract public boolean write(String[] username, int[] score);

    public void setFile(File file) {
        FileIO.file = file;
    }

    public File getFile() {
        return FileIO.file;
    }

    synchronized public LinkedList<User> getUserList() {
       /* if (Permission) {
            Permission = false;
            return listUsers;
        } else {
            return null;
        }*/
        return listUsers;
    }

    synchronized public String[][] getUserListasString() {
        //if (ListPermission) {
            //System.out.println("list permission = true");
            String[] username = new String[listUsers.toArray().length];
            String[] scores = new String[listUsers.toArray().length];
            Iterator iterator = listUsers.iterator();
            int i = 0;
            while (iterator.hasNext()) {
                User temp = (User)iterator.next();
                username[i] = temp.getUsername();
                scores[i] = Integer.toString(temp.getScore());
                i++;
            }
            String[][] return1 = new String[2][];
            return1[0] = username;
            return1[1] = scores;
            return return1;
    }

    protected boolean releaseList() {
        if ((this.Permission == ListPermission) == false) {
            return (ListPermission = true);
        }
        return false;
    }

    protected class User {
        {
            score = 0;
            username = "ZZZZZZZZZZ";
        }
        
        public int getScore() {
            return score;
        }

        public String getUsername() {
            return username;
        }
        private String username;
        private int score;

        public User(String username, int score) {
            this.username = username;
            this.score = score;
        }
    }
}
